#include <iostream>
#include <map>
#include <queue>
#include <algorithm>
using namespace std;

int t[1100];
int getHeight(int h){
	if( t[h]!= -1){
		return max(getHeight(h*2),getHeight(h*2+1)) + 1;
	}return 0;
}
int main(){
	freopen("in.txt","r",stdin);
	int n;
	cin >> n;
	map<int, pair<int,int> > tree;
	for( int i=1; i<=n; i++){
		int a,b;
		cin >> a >> b;
		tree[i] = make_pair(a,b);
	}
	t[1] = 1;
	queue<int> q;
	q.push(1);
	while( !q.empty()){
		int index = find(t,t+1100,q.front())-t;
		int left = tree[q.front()].first;
		int right = tree[q.front()].second;
		t[index*2] = left;
		if( left != -1 ) q.push(left);
		t[index*2+1] = right;  
		if( right != -1 ) q.push(right);
		q.pop();
	}
	cout << getHeight(1);
	return 0;
}
